import * as bm from "/js/baseModule.js";

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);
        let that = this;
        that.payMoney = 2500;
        this.find('.span-range').on("input", function (e) {
            var width = that.find(".span-range").width();
            //取得point个数
            //m% * (最大值 - 最小值) + 最小值;
            // let currSpan = this.value / 100 * 9000 + 1000;
            let currSpan =  Math.floor(this.value / 100 * 90) * 100 + 1000;
            //half-point 相对位置
            let hp = that.find(".half-point").width();
            let tp = that.find(".total-point").width();
            let a = (width * this.value / 100 - hp / 2) / width * 100;
            let b = (width * this.value / 100 - tp / 2) / width * 100;
            if (this.value <= 10) {
                a = a + 3;
                b = b + 3;
            } else if (this.value > 10 && this.value <= 20) {
                a = a + 2;
                b = b + 2;
            } else if (this.value > 20 && this.value <= 30) {
                a = a + 1;
                b = b + 1;
            } else if (this.value >= 60 && this.value < 70) {
                a = a - 1;
                b = b - 1;
            } else if (this.value >= 70 && this.value < 80) {
                a = a - 2;
                b = b - 2;
            } else if (this.value >= 80 && this.value <= 100) {
                a = a - 3;
                b = b - 3;
            }
            $('.blue-cover').css('width', this.value + '%');
            $('.half-point').css('left', a + '%').html(currSpan + "个" + '<em class="horn"></em>');
            $('.total-point').css('left', b + '%').html("&yen;" + currSpan * 0.9);
            that.payMoney = currSpan;
        }).on('change', function (e) {
            //取得当前个数
            let currSpan = Math.floor(this.value / 100 * 90) * 100 + 1000;

        });

    }
    afterInstall(){
        let content = this.config.parent.find('.CenterModalContent');
        let height = content.height();
        content.css('margin-top', -height / 2 + 'px');
        setTimeout(function () {
            content.css({'transform': 'scale(1,1)', 'opacity': '1'});
        }, 100);
    }
    /**充值*/
    _evt_recharge() {
        this._evt_goback();
        // if (globalData.user.half_price_paid) {
        //     //已享受半价优惠付款
        //     funs.tip("每个用户只能享受一次优惠", "", "fail");
        // } else {
            //充值
            /**
             * 微信支付
             */
            api.postForDiscountWxPay({ "bid_point": this.payMoney }, {
                succ: function (json) {
                    function onBridgeReady() {
                        WeixinJSBridge.invoke(
                            'getBrandWCPayRequest', json.wxpay,
                            function (res) {
                                // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回"ok"，但并不保证它绝对可靠。
                                if (res.err_msg == "get_brand_wcpay_request:ok") {
                                    api.GetMyPack({
                                        succ:function (data) {
                                            globalData.pack.id = data.pack.id;
                                            globalData.pack.accum_call_point = data.pack.accum_call_point;
                                            globalData.pack.accum_sms_point = data.pack.accum_sms_point;
                                            globalData.pack.accum_bid_point = data.pack.accum_bid_point;
                                            globalData.pack.bid_point = data.pack.bid_point;
                                            globalData.pack.call_point = data.pack.call_point;
                                            globalData.pack.sms_point = data.pack.sms_point;
                                            globalData.pack.end_date = data.pack.end_date;
                                            globalData.pack.prov_list = data.pack.prov_list;
                                            $("#ValueAddService .currency-count .count").text(model.globalData.pack.bid_point);
                                            funs.tip('购买成功', '', 'succ');
                                        }
                                    });
                                } else {
                                    //支付不成功
                                    // funs.tip('支付失败', res.err_msg, 'fail');
                                }
                            }
                        );
                    }
                    if (typeof WeixinJSBridge == "undefined") {
                        if (document.addEventListener) {
                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                        } else if (document.attachEvent) {
                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                        }
                    } else {
                        onBridgeReady();
                    }
                },
                fail: function (error) {
                    if (error.status == '41009002') {
                        funs.tip(error.msg, "请先去设置服务资料，成为工程商", "fail");
                    } else {
                        funs.tip(error.msg, "", "fail");
                    }
                }
            })
        // }
    }
}
